package com.ego.dao.impl;

import com.ego.dao.DressDao;
import com.ego.utils.JDBCUtils;
import com.ego.pojo.Product;
import com.ego.vo.ProductListVo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 袁梦达 2019012364
 */
public class DressDaoImpl implements DressDao {

    @Override
    public List<ProductListVo> getProductList(){
        List<ProductListVo> productList = new ArrayList<>();
        Connection connection= JDBCUtils.getConnection();
        PreparedStatement statement = null;
        try {
            String sql = "select * from product";
            statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                ProductListVo productListVo = new ProductListVo();
                productListVo.setId(resultSet.getInt("id"));
                productListVo.setBaseInformation(resultSet.getString("base_information"));
                productListVo.setPrice(resultSet.getDouble("price"));
                productListVo.setImage(resultSet.getString("image"));
                productList.add(productListVo);
            }
            return productList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(statement, connection);
        }
        return null;
    }

    @Override
    public List<ProductListVo> getProductDiscountList(){
        List<ProductListVo> productList = new ArrayList<>();
        Connection connection= JDBCUtils.getConnection();
        PreparedStatement statement = null;
        try {
            String sql = "select * from product where discount = 1";
            statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                ProductListVo productListVo = new ProductListVo();
                productListVo.setId(resultSet.getInt("id"));
                productListVo.setBaseInformation(resultSet.getString("base_information"));
                productListVo.setPrice(resultSet.getDouble("price"));
                productListVo.setImage(resultSet.getString("image"));
                productList.add(productListVo);
            }
            return productList;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(statement, connection);
        }
        return null;
    }

    @Override
    public Product getProduct(Integer productId){
        Connection connection= JDBCUtils.getConnection();
        PreparedStatement statement = null;
        try {
            String sql = "select * from product where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, productId);
            ResultSet resultSet = statement.executeQuery();
            if(resultSet.next()){
                Product product = new Product();
                product.setId(resultSet.getInt("id"));
                product.setBrand(resultSet.getString("brand"));
                product.setSize(resultSet.getInt("size"));
                product.setBaseInformation(resultSet.getString("base_information"));
                product.setDetail(resultSet.getString("detail"));
                product.setImage(resultSet.getString("image"));
                product.setDiscount(resultSet.getBoolean("discount"));
                product.setPrice(resultSet.getDouble("price"));
                return product;
            }else {
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(statement, connection);
        }
        return null;
    }
}
